package com.company.behavioral.state_pattern.concrete_state;

import com.company.behavioral.state_pattern.state.AccountState;

/**
 * 透支状态
 * @ClassName OverdraftState
 * @Author tiezheng.han
 * @Date 2020/9/7 14:04
 **/
public class OverdraftState extends AccountState {

    public OverdraftState(AccountState state){
        this.acc = state.acc;
    }

    @Override
    public void deposit(double amount) {
        acc.setBlanace(acc.getBlanace() + amount);
        stateCheck();
    }

    @Override
    public void withdraw(double amount) {
        if ((acc.getBlanace() - amount)>=-2000){
            acc.setBlanace(acc.getBlanace() - amount);
            stateCheck();
        } else {
            System.out.println("余额不足，只能取" + (2000+acc.getBlanace()));
        }
    }

    @Override
    public void computeInterest() {
        System.out.println("计算利息！");
    }

    /**
     * 状态转换
     */
    @Override
    public void stateCheck() {
        if (acc.getBlanace() > 0){
            acc.setState(new NormalState(this));
        } else if (acc.getBlanace() == -2000){
            acc.setState(new RestrictedState(this));
        } else if (acc.getBlanace() < -2000){
            System.out.println("操作受限！");
        }
    }
}
